package com.cxyd.modules.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cxyd.common.constant.enums.wms.OrderStatusEnum;
import com.cxyd.common.constant.enums.wms.PurchaseStatusEnum;
import com.cxyd.modules.wms.dao.OrderDao;
import com.cxyd.modules.wms.dao.OrderDetailDao;
import com.cxyd.modules.wms.dao.PurchaseDao;
import com.cxyd.modules.wms.dao.PurchaseDetailDao;
import com.cxyd.modules.wms.entity.OrderDetailEntity;
import com.cxyd.modules.wms.entity.OrderEntity;
import com.cxyd.modules.wms.entity.PurchaseDetailEntity;
import com.cxyd.modules.wms.entity.PurchaseEntity;
import com.cxyd.modules.wms.service.CommonService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class CommonServiceImpl implements CommonService {

    public final OrderDao orderDao;
    public final OrderDetailDao orderDetailDao;
    public final PurchaseDao purchaseDao;
    public final PurchaseDetailDao purchaseDetailDao;

    public CommonServiceImpl(OrderDao orderDao, OrderDetailDao orderDetailDao, PurchaseDao purchaseDao, PurchaseDetailDao purchaseDetailDao) {
        this.orderDao = orderDao;
        this.orderDetailDao = orderDetailDao;
        this.purchaseDao = purchaseDao;
        this.purchaseDetailDao = purchaseDetailDao;
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void purchaseStatus(Map<String, Object> params) {
        Long purchaseId = Long.parseLong(params.get("purchaseId").toString());
        Long userId = Long.parseLong(params.get("userId").toString());
        String comment = (String)params.get("comment");
        PurchaseEntity updatePurchase = new PurchaseEntity();
        updatePurchase.setPurchaseId(purchaseId);
//        updatePurchase.setComments(comment);
        updatePurchase.setUpdateDate(new Date());
        updatePurchase.setUpdater(userId);

        List<PurchaseDetailEntity> detailList = purchaseDetailDao.selectList(new QueryWrapper<PurchaseDetailEntity>().eq("purchase_id", purchaseId));
        List<PurchaseDetailEntity> completeList = detailList.stream()
                .filter(a -> PurchaseStatusEnum.COMPLETE.getCode() == a.getDetailSt()).collect(Collectors.toList());
        List<PurchaseDetailEntity> initList = detailList.stream()
                .filter(a -> PurchaseStatusEnum.INITIAL.getCode() == a.getDetailSt()).collect(Collectors.toList());
        //如果全是初始状态，主单初始
        if (!CollectionUtils.isEmpty(initList) && detailList.size() == initList.size()) {
            updatePurchase.setPurchaseSt(PurchaseStatusEnum.INITIAL.getCode());
            //如果全是完成状态，主单完成
        } else if (!CollectionUtils.isEmpty(completeList) && detailList.size() == completeList.size()) {
            updatePurchase.setPurchaseSt(PurchaseStatusEnum.COMPLETE.getCode());
            //否则收货中
        } else {
            updatePurchase.setPurchaseSt(PurchaseStatusEnum.RECEIVING.getCode());
        }
        purchaseDao.updateById(updatePurchase);
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void orderStatus(Map<String, Object> params) {
        Long orderId = Long.parseLong(params.get("orderId").toString());
        Long userId = Long.parseLong(params.get("userId").toString());
        String comment = (String)params.get("comment");
        OrderEntity updateOrder = new OrderEntity();
        updateOrder.setOrderId(orderId);
        updateOrder.setUpdater(userId);
//        updateOrder.setComments(comment);
        updateOrder.setUpdateDate(new Date());
        List<OrderDetailEntity> detailList = orderDetailDao.selectList(new QueryWrapper<OrderDetailEntity>().eq("order_id", orderId));
        List<OrderDetailEntity> completeList = detailList.stream().filter(a -> OrderStatusEnum.STOCK_COMPLETED.getCode() == a.getDetailSt()).collect(Collectors.toList());
        List<OrderDetailEntity> initList = detailList.stream().filter(a -> OrderStatusEnum.INITIAL.getCode() == a.getDetailSt()).collect(Collectors.toList());
        List<OrderDetailEntity> execList = detailList.stream().filter(a -> OrderStatusEnum.OUT_STOCK.getCode() == a.getDetailSt()).collect(Collectors.toList());
        List<OrderDetailEntity> execCompleteList = detailList.stream().filter(a -> OrderStatusEnum.OUT_COMPLETE.getCode() == a.getDetailSt()).collect(Collectors.toList());
        //全部初始，主单初始
        if (!CollectionUtils.isEmpty(initList) && detailList.size() == initList.size()) {
            updateOrder.setOrderSt(OrderStatusEnum.INITIAL.getCode());
            //全部备货完成，主单备货完成
        } else if (!CollectionUtils.isEmpty(completeList) && detailList.size() == completeList.size()) {
            updateOrder.setOrderSt(OrderStatusEnum.STOCK_COMPLETED.getCode());
            //全部出库完成，主单出库完成
        } else if (!CollectionUtils.isEmpty(execCompleteList) && detailList.size() == execCompleteList.size()) {
            updateOrder.setOrderSt(OrderStatusEnum.OUT_COMPLETE.getCode());
            //有一个出库中，主单出库中
        } else if (!CollectionUtils.isEmpty(execList)) {
            updateOrder.setOrderSt(OrderStatusEnum.OUT_STOCK.getCode());
            //否则备货中
        } else {
            updateOrder.setOrderSt(OrderStatusEnum.IN_STOCK.getCode());
        }
        orderDao.updateById(updateOrder);
    }
}
